package com.yhy;

import java.util.ArrayList;
import java.util.Set;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2017/11/17
 */
public class Component {
	private IGraph graph=null;
	private int count=0;


	public Component(IGraph graph){
		this.graph = graph;
	}

	public void dfs( Vertex vertex ){
		//设置此点已经遍历
		vertex.setVisted(true);

		ArrayList<Vertex> adjs = graph.getAdj(vertex);

		//遍历所有的临边
		for ( Vertex adj: adjs ){
			if( !adj.isVisted() ){
				dfs( adj );
			}
		}

	}

	public int component(){
		//连通分量的个数
		int count = 0;
		Set<Vertex> vertexs = graph.getVertexs();
		//从每个点分别开始进行深度遍历
		for ( Vertex ver : vertexs ){
			//每发现一个没有遍历的点 连通变量就++ 并且进行一个深度优先遍历
			if( !ver.isVisted() ){
				dfs(ver);
				count ++;
			}
		}
		return count;
	}

}